package com.zhuizikejithreeig.cn.threeigkeji.mianinfo;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.LinearLayout;

import com.zhuizikejithreeig.cn.been.MesBeen;
import com.zhuizikejithreeig.cn.been.ProblemBeen;
import com.zhuizikejithreeig.cn.content.UriContect;
import com.zhuizikejithreeig.cn.threeigkeji.BasicActivity;
import com.zhuizikejithreeig.cn.threeigkeji.CompontApplayCation;
import com.zhuizikejithreeig.cn.threeigkeji.R;
import com.zhuizikejithreeig.cn.utils.GsonUtil;
import com.zhuizikejithreeig.cn.utils.ToastMake;
import com.zhuizikejithreeig.cn.utils.Utils;
import com.zhuizikejithreeig.cn.weigit.MyButtonView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import okhttp3.MediaType;

public class EquipFixActivity extends BasicActivity implements View.OnClickListener {

    private GridLayout mGlideOne;
    private List<String>       mStrings        = new ArrayList<>();
    private List<MyButtonView> mButtonViewList = new ArrayList<>();
    private EditText mEditText;
    private Button   mButton;
    private int selectPostion = -1;
    private GridLayout mGlideTwo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_equip_fix);
        setLeftShow(1, R.mipmap.back);
        getLift_V().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //动态添加
        mGlideOne = (GridLayout) findViewById(R.id.gildeone);
        mGlideTwo = (GridLayout) findViewById(R.id.gildetwo);
        mEditText = (EditText) findViewById(R.id.edittext);
        mButton = (Button) findViewById(R.id.name8);

        mButton.setOnClickListener(this);

        if (isConnected()){
            haveProblemList();
        }



        mStrings.add("乘客要求退款");
        mStrings.add("人为损坏");
        mStrings.add("座椅扫码失败");
        for (int i = 0; i < mStrings.size(); i++) {
            MyButtonView buttonView = new MyButtonView(EquipFixActivity.this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            buttonView.setLayoutParams(params);
            buttonView.getTextView().setText(mStrings.get(i));
            mButtonViewList.add(buttonView);
            if (i % 2 == 0) {
                mGlideOne.addView(buttonView);
            } else {
                mGlideTwo.addView(buttonView);
            }
        }
        //可以增加动画或者改变其颜色等
        for (int i = 0; i < mButtonViewList.size(); i++) {
            final int b = i;
            mButtonViewList.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //设置成单选的
//                    othearDo();
                    selectPostion = b;
//                    mButtonViewList.get(selectPostion).getLinearLayout().setSelected(true);
                    //再次点击可以取消
                    doubleClike(selectPostion);

                }
            });
        }

    }

    private void doubleClike(int selectPostion) {

        boolean saveEnabled = mButtonViewList.get(selectPostion).getLinearLayout().isSelected();

        if (saveEnabled) {
            mButtonViewList.get(selectPostion).getLinearLayout().setSelected(false);
        }else {

            mButtonViewList.get(selectPostion).getLinearLayout().setSelected(true);
        }

    }

    private void haveProblemList() {

        showProgrees("加载中请稍等....");
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpUtils
                        .get()
                        .url(UriContect.BREAKDOWN)
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                ToastMake.showToast(getResources().getString(R.string.string58));
                                hideProgree();
                            }

                            @Override
                            public void onResponse(String response, int id) {
                                hideProgree();
                                ProblemBeen mesBeen = GsonUtil.parseJsonToBean(response, ProblemBeen.class);
                                if (mesBeen == null) {
                                    ToastMake.showToast(getResources().getString(R.string.string58));
                                    return;
                                }
                                if (!TextUtils.equals(mesBeen.getStatus(), "1")) {
                                    ToastMake.showToast(mesBeen.getMag());
                                    return;
                                } else {

                                    for (ProblemBeen.NameBean nameBean : mesBeen.getName()) {
                                        mStrings.add(nameBean.getBreakdown_name());
                                    }

                                    //切换到主线程
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            //动态添加按钮
//                                            for (int i = 0; i < mStrings.size(); i++) {
//                                                MyButtonView buttonView = new MyButtonView(EquipFixActivity.this);
//                                                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//                                                buttonView.setLayoutParams(params);
//                                                buttonView.getTextView().setText(mStrings.get(i));
//                                                mButtonViewList.add(buttonView);
//                                                if (i % 2 == 0) {
//                                                    mGlideOne.addView(buttonView);
//                                                } else {
//                                                    mGlideTwo.addView(buttonView);
//                                                }
//                                            }
//                                            //可以增加动画或者改变其颜色等
//                                            for (int i = 0; i < mButtonViewList.size(); i++) {
//                                                final int b = i;
//                                                mButtonViewList.get(i).setOnClickListener(new View.OnClickListener() {
//                                                    @Override
//                                                    public void onClick(View v) {
//                                                        //设置成单选的
//                                                        othearDo();
//                                                        selectPostion = b;
//                                                        mButtonViewList.get(b).getLinearLayout().setSelected(true);
//
//                                                    }
//                                                });
//                                            }
                                        }
                                    });
                                }
                            }
                        });
            }
        }).start();

    }

    private void othearDo() {
        for (int i = 0; i < mButtonViewList.size(); i++) {
            mButtonViewList.get(i).getLinearLayout().setSelected(false);
        }
    }

    @Override
    public void onClick(View v) {
        String string = mEditText.getText().toString();
        if (string == null) {
            ToastMake.showToast(getResources().getString(R.string.string88));
            return;
        }
        if (selectPostion == -1) {
            ToastMake.showToast(getResources().getString(R.string.string87));
            return;
        }
        if (isConnected()) {
            postToMain();
        }
    }

    private void postToMain() {

        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        showProgrees("提交中,请稍等.....");
                        HashMap<String, String> map = new HashMap<>();
                        map.put("reg_id", CompontApplayCation.phone);
                        map.put("phone", CompontApplayCation.phone);
                        String json = GsonUtil.parseMapToJson(map);
                        showProgrees(getResources().getString(R.string.string75));
                        OkHttpUtils
                                .postString()
                                .url(UriContect.BREAKDOWN)
                                .content(json)
                                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Call call, Exception e, int id) {
                                        ToastMake.showToast(getResources().getString(R.string.string58));
                                        hideProgree();
                                    }

                                    @Override
                                    public void onResponse(String response, int id) {
                                        hideProgree();
                                        MesBeen mesBeen = GsonUtil.parseJsonToBean(response, MesBeen.class);
                                        if (mesBeen == null) {
                                            ToastMake.showToast(getResources().getString(R.string.string58));
                                            return;
                                        }
                                        if (!TextUtils.equals(mesBeen.getError_code(), "0")) {
                                            ToastMake.showToast(mesBeen.getMsg());
                                            return;
                                        } else {
                                            Utils.runOnUIThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    finish();
                                                }
                                            });
                                        }
                                    }
                                });
                    }
                }
        ).start();
        finish();
    }
}

